package org.example.SlApi.QykbxxApi;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.codec.net.URLCodec;
import org.example.JycInfo;
import org.example.utils.ReadConf;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static org.example.CookieHandlingFilter.cookies;
import static org.example.RestClient.givenWithCookies;
import static org.example.RestClient.loginAndUpdateCookies;
import static org.example.SlApi.JbxxApi.JbxxbcApi.initJyclist;

public class QykbxxApi {//企业开办信息保存
    public static void main(String[] args) throws IOException {
        qykbxxApi();
    }
    private static List<JycInfo> JycList = new ArrayList<>();

    public static int qykbxxApi() throws IOException {
        // 登录并更新cookies

        loginAndUpdateCookies();

        RestAssured.baseURI = ReadConf.getHost(); // 设置基础URI
        List<JycInfo> jycInfoList = initJyclist(); // 调用方法来获取列表
        if (!jycInfoList.isEmpty()) {
            JycInfo firstJycInfo = jycInfoList.get(0); // 获取列表中的第一个元素
            String pripid = firstJycInfo.getPripid(); // 使用实际的getter方法
            String sbid = firstJycInfo.getSbid();
            String entname = firstJycInfo.getEntname();
            String qylxdl = firstJycInfo.getQylxdl();
            String nameid = firstJycInfo.getName_id();


            URLCodec urlCodec = new URLCodec();
            String encodedParam = URLEncoder.encode(entname, "UTF-8");

            String data = "method=edit&sbid="+sbid+"&pripid="+pripid+"&entname="+encodedParam+"&entid="+pripid+"&enttype=1140&regno=&regorg=330000&qylxdl=11&sbms=1&ljmsign=2&znspsign=0&bdcsign=&name_id="+nameid+"&S_ITE=0&S_ITENAME=&S_ITEREGNO=&gzkz=S_PRIPID_%3A"+pripid+"_%2CS_RECODEMARK_%3A1_%2CS_PRIPID_GZ_%3A330100888888_%2CS_ISMAIL_%3A1_%2CS_MAILTYPE_%3Aems_%2CS_ISFREE_%3A1_%2CS_MAILNAME_%3A%E5%91%A8%E5%91%A8_%2CS_MAILTEL_%3A13676858355_%2CS_MAILDOM_PROVINCE_%3A330000_%2CS_MAILDOM_CITY_%3A330200_%2CS_MAILDOM_DISTRICT_%3A330102_%2CS_MAILDOM_LOCATION_%3A%E6%B8%85%E6%B3%A2%E8%A1%97%E9%81%93%E9%AB%98%E7%AB%AF%E6%8A%A4%E8%82%A4%E7%9A%84_%2CS_POSTALCODE_%3A310000_%2CS_MAILDOM_%3A%E6%B5%99%E6%B1%9F%E7%9C%81%E6%9D%AD%E5%B7%9E%E5%B8%82%E4%B8%8A%E5%9F%8E%E5%8C%BA%E6%B8%85%E6%B3%A2%E8%A1%97%E9%81%93%E9%AB%98%E7%AB%AF%E6%8A%A4%E8%82%A4%E7%9A%84&yhkh=S_PRIPID_%3A"+pripid+"_%2CS_RECODEMARK_%3A0_%2CS_BANKID_%3A&sksb=S_PRIPID_%3A"+pripid+"_%2CS_ISTICKET_%3A1_%2CS_RECODEMARK_%3A1&sbdj=S_PRIPID_%3A"+pripid+"_%2CS_ISSECURE_%3A1_%2CS_CONCACTNAME_%3A%E6%A0%97%E4%BC%9A_%2CS_CONCACTMOBTEL_%3A13568547854_%2CS_ISZGCB_%3A0&ybdj=S_PRIPID_%3A"+pripid+"_%2CS_IS_MEDICAL_INSURANCE_%3A1_%2CS_MEDICAL_INSURANCE_AGENTNAME_%3A%E6%A0%97%E4%BC%9A_%2CS_MEDICAL_INSURANCE_AGENTMOBTEL_%3A13568547854_%2CS_MEDICAL_INSURANCE_TYPE_%3A310%2C510%2C_%2CS_YWLY_%3A1&gjjzgdj=&gjj=S_PRIPID_%3A"+pripid+"_%2CS_YWLY_%3A1_%2CS_RECODEMARK_%3A1_%2CS_DWJCBL_%3A12%25_%2CS_GRJCBL_%3A12%25_%2CS_DWFXR_%3A12&S_BANKID=&RECODEMARK_GZ=1&ENTNAME_GZ=%E6%9D%AD%E5%B7%9E%E5%B8%82***%E6%89%B9%E6%9C%8D%E5%8A%A1%E7%AE%A1%E7%90%86%E5%8A%9E%E5%85%AC%E5%AE%A4&S_PRIPID_GZ=330100888888&YZXX_IDS=&S_ISFREE=1&ISMAIL=1&MAILTYPE=ems&S_MAILDOM_PROVINCE=330000&S_MAILDOM_CITY=330200&S_MAILDOM_DISTRICT=330102&S_MAILDOM_LOCATION=%E6%B8%85%E6%B3%A2%E8%A1%97%E9%81%93%E9%AB%98%E7%AB%AF%E6%8A%A4%E8%82%A4%E7%9A%84&S_POSTALCODE=310000&ISTICKET=1&ISZGCB=0&S_DWJCBL=12%25&S_GRJCBL=12%25&S_DWFXR=12&ISZGCB_GJJ=0&S_MEDICAL_INSURANCE_TYPE=310&S_MEDICAL_INSURANCE_TYPE=510&ISZGCB_YB=0&csrftoken=&csrftoken=";
            Response response = givenWithCookies()

                    .header("Content-Type","application/x-www-form-urlencoded")
//               .cookies(cookies)
                    .body(data)
                    .log().all()
                    .post("ywtSldj.do?method=saveQykbxx&type=1");
            System.out.println(cookies);
            int statusCode = Integer.parseInt(String.valueOf(response.getStatusCode()));
            System.out.println(statusCode);
            String body = response.getBody().asString();
            System.out.println(body);
            if (statusCode == 302) {

                String redirectUrl = response.getHeader("Location");
                System.out.println(redirectUrl);
                if (redirectUrl != null) {
                    Response finalResponse = givenWithCookies()
                            .log().all()
                            .when()
                            .get(redirectUrl);
                    int statusCode1 = finalResponse.getStatusCode();
                    System.out.println("Status Code: " + statusCode1);


                    return 0;

                }
                return 0;
            }}
        return 0;
    }
}
